package com.ts.proj.service.impl;

import com.ts.base.service.impl.CommonServiceImpl;
import com.ts.proj.entity.SystemButtonEntity;
import com.ts.proj.repository.SystemButtonRepository;
import com.ts.proj.service.SystemButtonService;
import com.ts.util.PageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
public class SystemButtonServiceImpl extends CommonServiceImpl<SystemButtonEntity> implements SystemButtonService {

    @Autowired
    private SystemButtonRepository systemButtonRepository;

    @Override
    public List<SystemButtonEntity> findAll(PageData pageData, Integer start, Integer length) {
        Specification<SystemButtonEntity> sp = this.getSpecification(pageData,SystemButtonEntity.class);
        Sort sort = new Sort(Sort.Direction.ASC,"id");
        Pageable pageable = new PageRequest(start, length, sort);
        return systemButtonRepository.findAll(sp,pageable).getContent();
    }

    @Override
    public long count(PageData pageData) {
        Specification<SystemButtonEntity> sp = this.getSpecification(pageData,SystemButtonEntity.class);
        return systemButtonRepository.count(sp);
    }

    @Override
    public boolean deleteByIds(Iterable<SystemButtonEntity> iterable) {
        systemButtonRepository.deleteInBatch(iterable);
        return false;
    }

    @Override
    public List<SystemButtonEntity> saveAll(List<SystemButtonEntity> systemButtonEntities) {
        return systemButtonRepository.saveAll(systemButtonEntities);
    }

    @Override
    public List<SystemButtonEntity> findAllById(Integer[] ids) {
        List<SystemButtonEntity> list = systemButtonRepository.findAllById(Arrays.asList(ids));
        if(list.size()==0){
            SystemButtonEntity systemButtonEntity = new SystemButtonEntity();
            list.add(systemButtonEntity);
        }
        return list;
    }

    @Override
    public boolean deleteAll() {
        return false;
    }

    @Override
    public List<SystemButtonEntity> findSystemButtonLocalQuery(String menuURL) {
        return systemButtonRepository.findSystemButtonLocalQuery(menuURL);
    }
}
